package org.example.uuadb.config;

import lombok.extern.slf4j.Slf4j;
import org.example.uuadb.entity.OauthClientDetail;
import org.example.uuadb.mapper.ClientMapper;
import org.example.uuadb.util.DateTimeUtil;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.NoSuchClientException;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author : yurui
 * @date : 2024/6/3
 * @TIME : 21:56
 * @project : AuthDemo
 * 自定义从数据库中获取客户端信息
 */
@Slf4j
public class DBClientDetailsService implements ClientDetailsService {

    @Resource
    private ClientMapper clientMapper;

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {

        log.info("请求的客户端ID={},时间={}",clientId, DateTimeUtil.sqlDate2DateStr(new Date()));

        OauthClientDetail clientDetail = clientMapper.selectClientByClientId(clientId);

        if (clientDetail == null){
            throw new NoSuchClientException("No client with requested id: " + clientId);
        }

        return new DBClientDetails(clientDetail);
    }
}
